﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class UISystem : MonoBehaviour
{
    private Dictionary<string, GameObject> _dictionary;

    public string last;
    public object param;
    
    void Awake()
    {
        _dictionary = new Dictionary<string, GameObject>();
    }

    void Start()
    {
        Engine.UISystem = this;

        Engine.UISystem.Load("Login");
    }
	
	void Update()
    {
	
	}

    public void Next(string name, object param = null)
    {
        this.param = param;
        UnLoad(last);
        Load(name);
    }

    public GameObject Load(string name)
    {
        Object origin = Resources.Load("UI/" + name);
        return Load(name, origin);
    }

    public GameObject Load(string name, Object origin)
    {
        Vector3 position = new Vector3(Screen.width / 2, Screen.height / 2);
        GameObject gameObject = Instantiate(origin, position, new Quaternion(), transform) as GameObject;
        _dictionary.Add(name, gameObject);
        last = name;
        return gameObject;
    }

    public void UnLoad(string name)
    {
        Destroy(_dictionary[name]);
        _dictionary.Remove(name);
    }
}
